/* Copyright 2017 The Apollo Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "modules/map/hdmap/adapter/odr_parser/signals_odr_parser.h"

#include <iomanip>
#include <string>
#include <vector>

#include "modules/map/hdmap/adapter/odr_parser/data_define.h"
#include "modules/map/hdmap/adapter/xml_parser/util_xml_parser.h"

namespace apollo {
namespace hdmap {
namespace adapter {

Status SignalsOdrParser::ParseSignals(const tinyxml2::XMLElement& xml_node,
                               Road& road_internal)
{

  auto signals_node = xml_node.FirstChildElement("signals");
  if (signals_node) {
    auto signal_node = signals_node->FirstChildElement("signal");  
    while (signal_node) {
      RoadSignal signal;
      std::string signal_type;
      int checker =
          UtilXmlParser::QueryStringAttribute(*signal_node, "type", &signal_type);
      if (checker != tinyxml2::XML_SUCCESS) {
        std::string err_msg = "Error parse signal type.";
        return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
      }

      std::string upper_str = UtilXmlParser::ToUpper(signal_type);
      if (upper_str == "TRAFFICLIGHT") {
        signal.type = ROAD_TRAFFICLIGHT;
      } else {
        AWARN << "Warning: not supported signal type!";
        signal_node = signal_node->NextSiblingElement("signal");
        continue;
      }

      RETURN_IF_ERROR(ParseTrafficLights(*signal_node, signal));

      road_internal.signals.push_back(signal);
      signal_node = signal_node->NextSiblingElement("signal");
    }
  }

  return Status::OK();
}

Status SignalsOdrParser::ParseTrafficLights(
                  const tinyxml2::XMLElement& xml_node, RoadSignal& signal) {
  int checker;
  checker = UtilXmlParser::QueryStringAttribute(xml_node, "name", &signal.name);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal type attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  // id
  checker = UtilXmlParser::QueryStringAttribute(xml_node, "id", &signal.id);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal id attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("s", &signal.s);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal s attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("t", &signal.t);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal t attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("zOffset", &signal.zOffset);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal zOffset attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = UtilXmlParser::QueryStringAttribute(xml_node, "orientation", &signal.orientation);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal orientation attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("width", &signal.width);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal width attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("height", &signal.height);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal height attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("pitch", &signal.pitch);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal pitch attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  checker = xml_node.QueryDoubleAttribute("roll", &signal.roll);
  if (checker != tinyxml2::XML_SUCCESS) {
    std::string err_msg = "Error parse signal roll attribute";
    return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
  }

  auto userdata_node = xml_node.FirstChildElement("userData");
  if (userdata_node) {
    auto tag_node = userdata_node->FirstChildElement("tag");
    while (tag_node) {
      std::string key;
      checker = UtilXmlParser::QueryStringAttribute(*tag_node, "k", &key);
      if (checker != tinyxml2::XML_SUCCESS) {
        std::string err_msg = "Error parse signal tag key attribute";
        return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
      }

      std::string value;
      checker = UtilXmlParser::QueryStringAttribute(*tag_node, "v", &value);
      if (checker != tinyxml2::XML_SUCCESS) {
        std::string err_msg = "Error parse signal tag value attribute";
        return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
      }

      if (key.find("subSignal") == 0) {        
        std::string subId;
        if (key.find("subSignalId") == 0) {
          subId = key.erase(0, 11);
        } else {
          subId = key.erase(0, 9);
        }
        int sub_id = std::atoi(subId.c_str());
        if (sub_id == 0) {
          std::string err_msg = "Error parse subsignal id";
          return Status(apollo::common::ErrorCode::HDMAP_DATA_ERROR, err_msg);
        }

        if (signal.userData.subSignalIds.size() < sub_id) {
          signal.userData.subSignalIds.resize(sub_id);
        }
        if (signal.userData.subSignalTypes.size() < sub_id) {
          signal.userData.subSignalTypes.resize(sub_id);
        }
        if (key.find("subSignalId") == 0) {
          signal.userData.subSignalIds[sub_id - 1] = value;
        } else {
          signal.userData.subSignalTypes[sub_id - 1] = value;
        }
      } else {
        signal.userData.tags.push_back(std::make_pair(key, value));
      }

      tag_node = tag_node->NextSiblingElement("tag");
    }

    assert(signal.userData.subSignalIds.size() == signal.userData.subSignalTypes.size());
  }

  return Status::OK();
}

}  // namespace adapter
}  // namespace hdmap
}  // namespace apollo
